package org.jeecg.modules.wlhy.complaint.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.wlhy.complaint.entity.HyComplaint;
import org.jeecg.modules.wlhy.complaint.mapper.HyComplaintMapper;
import org.jeecg.modules.wlhy.complaint.service.IHyComplaintService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.CodeUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description: 投诉表
 * @Author: jeecg-boot
 * @Date:   2020-12-16
 * @Version: V1.0
 */
@Service
public class HyComplaintServiceImpl extends ServiceImpl<HyComplaintMapper, HyComplaint> implements IHyComplaintService {
    @Autowired
    private IHyCustomerService hyCustomerService;
    @Autowired
    private IHyDriverService hyDriverService;
    @Autowired
    private IHyShippingNoteService hyShippingNoteService;
    @Autowired
    private CodeUtil codeUtil;

    @Override
    public int getCountByDate(String dateString) {
        return baseMapper.getCountByDate(dateString);
    }

    // 创建投诉
    @Override
    public Result<?> create(HyComplaint hyComplaint){
        //生成投诉单号
        String num = codeUtil.createComplaintNum();
        hyComplaint.setComplaintNumber(num);
        //设置投诉状态(0待受理1未受理2待举证3处理中4已完结)
        hyComplaint.setComplaintStatus("0");
        //设置满意度
        hyComplaint.setEvaluationValue("0");

        String shippingNoteId = hyComplaint.getShippingNoteId();
        HyShippingNote hyShippingNote = hyShippingNoteService.getById(shippingNoteId);
        if(hyShippingNote == null) return Result.error("关联运单不存在：" + shippingNoteId);
        // 设置关联货源id
        hyComplaint.setOrderId(hyShippingNote.getOrderId());

        //设置投诉人
        LoginUser loginUser = UserUtil.getLoginUser();
        String userType = loginUser.getUType();
        String userId = loginUser.getId();
        //设置投诉人类型
        hyComplaint.setComplainantType(userType);
        //1管理员 2货主 4司机
        if(userType.equals("1")){
            hyComplaint.setComplainant(userId);//设置投诉人id
            hyComplaint.setComplainantName(loginUser.getRealname());//设置投诉人名称
        }else if(userType.equals("2")){
            QueryWrapper<HyCustomer> customerWrapper = new QueryWrapper<>();
            customerWrapper.eq("user_id", userId);
            HyCustomer customer = hyCustomerService.getOne(customerWrapper);
            if(customer == null) return Result.error("货主数据不存在！");
            hyComplaint.setComplainant(customer.getId());//设置货主id
            hyComplaint.setComplainantName(customer.getCustomerName());//设置投诉人名称
        }else if(userType.equals("4")){
            QueryWrapper<HyDriver> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            HyDriver driver = hyDriverService.getOne(queryWrapper);
            if(driver == null) return Result.error("司机数据不存在！");
            hyComplaint.setComplainant(driver.getId());//设置司机id
            hyComplaint.setComplainantName(driver.getDriverName());//设置投诉人名称
        }else{
            return Result.error("不存在的用户类型: " + userType);
        }
        //设置被投诉人名称
        String complaintAt = hyComplaint.getComplaintAt();
        // 1司机 2货主 3管理员
        if(complaintAt.equals("1")){
            HyDriver driver = hyDriverService.getById(hyComplaint.getRespondent());
            if(driver == null) return Result.error("被投诉人数据不存在：" + hyComplaint.getRespondent());
            hyComplaint.setRespondentName(driver.getDriverName());
        }else if(complaintAt.equals("2")){
            //司机投诉货主
            HyCustomer customer = hyCustomerService.getById(hyComplaint.getRespondent());
            if(customer == null) return Result.error("被投诉人数据不存在：" + hyComplaint.getRespondent());
            hyComplaint.setRespondentName(customer.getCustomerName());
        }else if(complaintAt.equals("3")){
            hyComplaint.setRespondentName("一六八网络货运平台");
        }else {
            return Result.error("当前投诉人类型不是货主或司机：" + userType);
        }

        save(hyComplaint);
        return Result.OK(hyComplaint);
    }
    // 审核投诉
    @Override
    public Result<?> check(HyComplaint hyComplaint){
        //评价时间
        hyComplaint.setCheckTime(new Date());//设置审核时间
        updateById(hyComplaint);
        return Result.OK(hyComplaint);
    }
    // 举证
    @Override
    public Result<?> proof(HyComplaint hyComplaint){
        if(!hyComplaint.getComplaintStatus().equals("2")){
            return Result.error("当前不可举证，请重进界面");
        }

        long start = hyComplaint.getCheckTime().getTime();//开始时间
        long now = new Date().getTime();//当前时间
        int timeLen = 3 * 24 * 60 * 60;//时长
        long leftTime = timeLen - (now - start) / 1000;//剩余时间
        if (leftTime <= 0){
            return Result.error("3天可举证时间已过，请重进界面！");
        }

        hyComplaint.setRespondentProofTime(new Date());//记录被投诉人举证时间
        hyComplaint.setComplaintStatus("3");//修改投诉状态为待处理
        updateById(hyComplaint);
        return Result.OK(hyComplaint);
    }
    // 举证结束
    @Override
    public Result<?> proofEnd(HyComplaint hyComplaint){
        if(!hyComplaint.getComplaintStatus().equals("2")){
            return Result.OK("当前非举证状态!");
        }
        hyComplaint.setComplaintStatus("3");
        updateById(hyComplaint);
        return Result.OK(hyComplaint);
    }
    // 处理
    @Override
    public Result<?> handle(HyComplaint hyComplaint){
        hyComplaint.setHandleTime(new Date());//处理时间记录
        updateById(hyComplaint);
        return Result.OK(hyComplaint);
    }
    // 完结投诉
    @Override
    public Result<?> finish(HyComplaint hyComplaint){
        // 处理中
        if (hyComplaint.getResponsibilityBelong() == null){
            return Result.error("未进行责任界定，不可以完结投诉!");
        }
        hyComplaint.setComplaintStatus("4");
        hyComplaint.setFinishTime(new Date());
        updateById(hyComplaint);
        return Result.OK(hyComplaint);
    }
    // 满意度评价
    @Override
    public Result<?> evaluate(HyComplaint hyComplaint){
        if (Integer.parseInt(hyComplaint.getEvaluationValue()) < 1) {
            return Result.error("请至少选择1颗星星！");
        }
        //评价时间
        hyComplaint.setEvaluateTime(new Date());
        updateById(hyComplaint);
        return Result.OK(hyComplaint);
    }
}
